import urllib2
import re
import sys

fields = "Team,Year,TotalGames,Wins,Losses,WinningPct,Possessions,PossessionsPer40minutes,FloorPct,Efficiency,FieldGoalsMade,FieldGoalAttempts,FieldGoalPct,FreeThrowsMade,FreeThrowAttempts,FreeThrowPct,3-ptFieldGoalsMade,3-ptFieldGoalAttempts,3-ptFieldGoalPct,EffectiveFieldGoalPct,TrueShootingPct,FreeThrowRate,FieldGoalPointPct,FreeThrowPointPct,3-ptFieldGoalPointPct,PointsPerPossessions,Points,PointsPerGame,ReboundPct,TotalRebounds,TotalReboundsPerGame,OffensiveRebPct,OffensiveRebounds,OffensiveReboundsPerGame,DefensiveRebPct,DefensiveRebounds,DefensiveReboundsPerGame,TeamRebounds,TeamReboundsPerGame,AssistPct,Assists,AssistsPerGame,AssisttoTurnover,StealPct,Steals,StealsPerGame,TurnoverPct,Turnovers,TurnoversPerGame,BlockPct,Blocks,BlocksPerGame,Fouls,FoulsPerGame\n"


for year in range (2012,2013):
	outfile = open("teamstats_"+str(year)+".csv",'w')
	outfile.write(fields)
	
	teams = [line.strip() for line in open("all-teams-"+str(year)+".csv")]
	
	count = 1
	
	for t in teams:
		t = t.lower()
		t = re.sub(r' ','-',t)
		t = re.sub(r'[^\w-]','',t)
		
		print str(year)+" "+str(count)+"/64, "+t
		
		try:
			req = urllib2.Request("http://statsheet.com/mcb/teams/"+t+"/team_stats?season="+str(year-1)+"-"+str(year), None, {'User-agent' : 'Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.1.5) Gecko/20091102 Firefox/3.5.5'})
			response = urllib2.urlopen(req)
			html = response.read()
			response.close()
			
			html = "".join(html.split()) # remove all whitespace
			tbegin = html.index("<tbody>") + len("<tbody>")
			tend = html.index("</tbody>")

			html = html[tbegin:tend] # remove everything except table data

			data = re.findall(r'<tr><td>([\w-]+)</td><td>(\d*\.?\d*)</td>',html)
			
			outfile.write(t+","+str(year))
			
			for d in data:
				if (d[0]!="Disqualifications"):
					outfile.write(","+str(d[1]))
			outfile.write("\n")

		except ValueError:
			print "NAME ERROR: "+t+" "+str(year)
		
		count += 1
			
	outfile.close()

